﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Security.Permissions;
using System.Threading;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Reflection;

namespace XF_GPS
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Main_Form : Form
    {
        public Main_Form()
        {
            InitializeComponent();
        }

        #region 窗体重载
        string us_name = "";
        string us_zuzhi = "";
        public Main_Form(string userName,string getip,string zuzhi,DataTable dtc)
        {
            InitializeComponent();
            sousuoTable = dtc;
            setTextBoxCallback = new SetTextBoxCallback(SetTextBox);
            setTextBoxCallback1 = new SetTextBoxCallback1(SetTextBox1);
            setTextBoxCallbackjk = new SetTextBoxCallbackjk(SetTextBoxjk);
            us_name = userName;
            us_zuzhi = zuzhi;

            DataTable dt = SqlHelper.Sqlselect("select * from AdminInfo where admin_name='" + userName + "'", "AdminInfo");
            if (dt.Rows.Count > 0)
            {
                string pifuStr = dt.Rows[0]["admin_pifu"].ToString();
                skinUI1.LoadSkinFile(pifuStr);

                if (pifuStr == "skins\\wmpx-XMP2.skn")
                {
                    waiguan1_ToolStripMenuItem.Checked = true;
                    waiguan2_ToolStripMenuItem.Checked = false;
                    waiguan3_ToolStripMenuItem.Checked = false;
                }
                if (pifuStr == "skins\\Watercolor-OLIVE.skn")
                {
                    waiguan1_ToolStripMenuItem.Checked = false;
                    waiguan2_ToolStripMenuItem.Checked = true;
                    waiguan3_ToolStripMenuItem.Checked = false;
                }
                if (pifuStr == "skins\\ClearLook-SYSTEM.skn")
                {
                    waiguan1_ToolStripMenuItem.Checked = false;
                    waiguan2_ToolStripMenuItem.Checked = false;
                    waiguan3_ToolStripMenuItem.Checked = true;
                }
            }
            
            toolStripStatusLabel1.Text = "用 户 : "+userName+"    组织："+zuzhi+"|  ";

            System.Net.WebClient wc = new System.Net.WebClient();
            toolStripStatusLabel4.Text = "登陆IP:" + getip + "  |  ";
        }
       
        //显示时间
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.toolStripStatusLabel3.Text = "当前时间 : " + DateTime.Now.ToString() + "  |  ";
        }
        #endregion

        public Panel pBLb; //自定义地图加载时 控制panel
        DataTable sousuoTable = null;

        #region Main_Form_Load
        private void Main_Form_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex =1;
            this.toolStripButton1.SelectedIndex = 0;

            车辆管理ToolStripMenuItem.Visible = false;

            //设置ToolStripMenuItem.Visible = false;
            //报警开关设置ToolStripMenuItem.Visible = false;
            //监控区域管理ToolStripMenuItem.Visible = false;
            监控权限设置ToolStripMenuItem.Visible = false;
            监控站点设置ToolStripMenuItem.Visible = false;

            //车辆行驶状态报表ToolStripMenuItem.Visible = false;
            车辆里程统计日报表ToolStripMenuItem.Visible = false;
            车辆里程统计月报表ToolStripMenuItem.Visible = false;
            拍照报表ToolStripMenuItem.Visible = false;
            进出站点报表ToolStripMenuItem.Visible = false;
            车辆行驶记录报表ToolStripMenuItem.Visible = false;

            this.splitContainer2.Panel2Collapsed = false; //隐藏实时信息窗口
            treeView2.SelectedNode = treeView2.Nodes[0];
            //this.toolStrip5.Visible = false;
            this.toolStripButton14.Visible = false;
            toolStripStatusLabel2.Text = "车辆数 :  正在载入车辆...  |  ";
            JiaZaiLoad();
            
            Thread myThread = new Thread(Loadmaps);
            object ob = this.webBrowserMap;
            myThread.Start(ob);

            JiaZai_CheLoad();
            Thread threadReceive1 = new Thread(new ThreadStart(Get_organization));
            threadReceive1.Start();

            BaoJingTable = new DataTable();
            BaoJingTable.Columns.Add("cph");//车牌号
            BaoJingTable.Columns.Add("bjlx");//报警类型
            BaoJingTable.Columns.Add("sd");//速度
            BaoJingTable.Columns.Add("sj");//司机
            BaoJingTable.Columns.Add("bjsj");//报警时间
            BaoJingTable.Columns.Add("bz");//备注

            foreach (TreeNode CNode in this.treeView2.Nodes)
            {
                if (CNode.Text == "直辖市")
                {
                    CNode.Expand();
                    return;
                }
            }
        }
        #endregion

        #region 加载地图
        public void Loadmaps(Object webBrowser)
        {
            //方法一
            //StreamReader sr = null;
            //try
            //{
            //    WebBrowser webbr = webBrowser as WebBrowser;
            //    string str = null;
            //    sr = new StreamReader("HTMLPage1.htm", Encoding.Default);
            //    webBrowserMap.Navigate("about:blank");
            //    //webBrowserMap.Document.OpenNew(false);
            //    str = sr.ReadToEnd();
            //    webbr.DocumentText = str;
            //    webbr.ObjectForScripting = this;

            //}
            //catch { MessageBox.Show("     加载地图失败!", "提示"); Loadmaps(webBrowser); }
            //finally
            //{
            //    sr.Close();
            //}
            //方法二
            string url = Application.StartupPath + "/GoogelMap.htm";
            WebBrowser webbr = webBrowser as WebBrowser;
            webbr.Navigate(url);
            webbr.ObjectForScripting = this;

            //string url = "http://192.168.1.194/service/HTMLPage1.htm";
            //WebBrowser webbr = webBrowser as WebBrowser;
            //webbr.Navigate(url);
            //webbr.ObjectForScripting = this;
        }


        //地图加载完毕后处理的事情
        private void Map_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.pBLb.Visible = false;
            this.menuStrip1.Enabled = true;
            this.toolStrip1.Enabled = true;
            this.toolStrip2.Enabled = true;
            this.toolStrip3.Enabled = true;
            this.toolStrip4.Enabled = true;
            this.toolStrip5.Enabled = true;
            
            this.timer2.Enabled = true;
        }
        //加载地图所有标注
        public void JiaZai_Biaozhu(WebBrowser webs)
        {
            DataTable biaozhu_dt = SqlHelper.Sqlselect("select * from DiTu_BiaoZhu", "DiTu_BiaoZhu");
            if (biaozhu_dt.Rows.Count > 0)
            {
                for (int i = 0; i < biaozhu_dt.Rows.Count; i++)
                {
                    string bzName= biaozhu_dt.Rows[i]["bz_name"].ToString();
                    string bzImage = Application.StartupPath + "\\Image\\" + biaozhu_dt.Rows[i]["bz_Image"].ToString();
                    int bzFontSize = int.Parse(biaozhu_dt.Rows[i]["bz_fontsize"].ToString());
                    bool bzBold=bool.Parse(biaozhu_dt.Rows[i]["bz_bold"].ToString());
                    bool bzBorder=bool.Parse(biaozhu_dt.Rows[i]["bz_border"].ToString());
                    string bzLngX = biaozhu_dt.Rows[i]["bz_jindu"].ToString();
                    string bzLatY = biaozhu_dt.Rows[i]["bz_weidu"].ToString();
                    string makerid = biaozhu_dt.Rows[i]["mark_id"].ToString();

                    //调用js里的方法添加标注
                    webs.Document.InvokeScript("abc_bq_biaozhu2", new object[] { bzFontSize, true, bzBold, bzBorder, bzName, bzImage, bzLngX, bzLatY, makerid });
                }
            }
        }

        //初始化地图默认位置
        public void JiaZai_set_cent(WebBrowser webs)
        {
            DataTable biaozhu_dt = SqlHelper.Sqlselect("select * from DiTu_ChongDW where userName='" + us_name + "'", "DiTu_ChongDW");
            if (biaozhu_dt.Rows.Count > 0)
            {
                string cent_x = biaozhu_dt.Rows[0]["cdw_lngx"].ToString();
                string cent_y = biaozhu_dt.Rows[0]["cdw_laty"].ToString();
                int get_zooml = int.Parse(biaozhu_dt.Rows[0]["cdw_zooml"].ToString());

                //调用js里的方法重定位
                webs.Document.InvokeScript("set_abc_cent", new object[] { cent_x, cent_y, get_zooml });

            }
        }

        //创建显示加载地图
        public void JiaZaiLoad()
        {
            int tabcheight = int.Parse((this.tabControl2.Size.Height / 2).ToString()) - 50;
            int tabwidth = int.Parse((this.tabControl2.Size.Width / 2).ToString()) - 100;

            pBLb = new Panel();
            pBLb.Location = new Point(tabwidth, tabcheight);
            pBLb.BackColor = System.Drawing.SystemColors.Window;
            pBLb.AutoSize = false;
            PictureBox pictureB = new PictureBox();
            try
            {
                pictureB.Image = Image.FromFile(Application.StartupPath+"\\Image\\indicator_medium.gif");
            }
            catch { }
            pictureB.Size = new Size(48, 37);
            pictureB.Location = new Point(0, 0);

            Label lb = new Label();
            lb.AutoSize = false;
            lb.Font = new System.Drawing.Font("宋体", 12.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            lb.Location = new System.Drawing.Point(45, 10);
            lb.Size = new System.Drawing.Size(166, 17);
            lb.Text = "正在加载地图.....";


            this.pBLb.Controls.Add(pictureB);
            this.pBLb.Controls.Add(lb);
            webBrowserMap.Controls.Add(pBLb);

        }

        //创建显示加载地图
        PictureBox pictureB1;
        Label lb1;
        public void JiaZai_CheLoad()
        {
            int tabcheight = int.Parse((this.tabControl1.Size.Height / 2).ToString())-70;
            int tabwidth = int.Parse((this.tabControl1.Size.Width / 2).ToString())-20;
            pictureB1 = new PictureBox();
            try
            {
                pictureB1.Image = Image.FromFile(Application.StartupPath + "\\Image\\indicator_medium.gif");
            }
            catch { }
            pictureB1.Size = new Size(48, 37);
            pictureB1.Location = new Point(tabwidth, tabcheight);

            lb1 = new Label();
            lb1.AutoSize = false;
            lb1.Font = new System.Drawing.Font("宋体", 12.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            lb1.Location = new Point(tabwidth-40, tabcheight+40);
            lb1.Size = new System.Drawing.Size(160, 17);
            lb1.Text = "正在载入车辆....";
            this.tabPage1.Controls.Add(pictureB1);
            this.tabPage1.Controls.Add(lb1);
        }  
        
        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Enabled = false;
            JiaZai_set_cent(webBrowserMap);
            JiaZai_Biaozhu(webBrowserMap);
            
        }
        #endregion

        #region 修改密码
        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UapdatUserPwd upp = new UapdatUserPwd(us_name,us_zuzhi);
            upp.ShowDialog();
        }
        #endregion

        #region 查看菜单
        private void 车辆列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (车辆列表ToolStripMenuItem.Checked == true)
            {
                this.splitContainer1.Panel1Collapsed = false;
            }
            else
            {
                this.splitContainer1.Panel1Collapsed = true;
            }
        }

        private void 实时信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.实时信息ToolStripMenuItem.Checked == true)
            {
                this.splitContainer2.Panel2Collapsed = false;
            }
            else
            {
                this.splitContainer2.Panel2Collapsed = true;
            }
        }

        private void 当前地图区域类的车辆ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        #region 公司车辆
        private void 行驶ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 停车ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 熄火ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 定位中ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 待安装ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #endregion

        #region 系统菜单
        private void 注销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否注销！", "提示！", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            else
            {
                Login lo = new Login();
                this.Hide();
                lo.Show();
            }

            //if (MessageBox.Show("要重新启动嘛？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.OK)
            //{
            //    Application.Exit();
            //    System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
            //}
        }

        private void 报警声音设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BaoJing_ShengYin bjsy = new BaoJing_ShengYin();
            int x = webBrowserMap.PointToScreen(new Point()).X - 5;
            int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
            bjsy.Location = new Point(x, y);
            int height = webBrowserMap.Size.Height;
            bjsy.Size = new Size(480, webBrowserMap.Size.Height);
            bjsy.ShowDialog();
        }

        private void 重新加载车辆ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            //toolStripStatusLabel2.Text = "车辆数 :  正在载入车辆...  |  ";
            JiaZaiLoad();

            Thread myThread = new Thread(Loadmaps);
            object ob = this.webBrowserMap;
            myThread.Start(ob);

            //JiaZai_CheLoad();
            //Thread threadReceive1 = new Thread(new ThreadStart(Get_organization));
            //threadReceive1.Start();

        }        
         private void 浏览器选项ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InternetChange ic = new InternetChange();
            ic.ShowDialog();
        }
        #region 更换外观
        private void waiguan1_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (waiguan1_ToolStripMenuItem.Checked==true)
            {
                waiguan2_ToolStripMenuItem.Checked = false;
                waiguan3_ToolStripMenuItem.Checked = false;
            }
            skinUI1.LoadSkinFile("skins\\wmpx-XMP2.skn");
            SqlHelper.ExcuteSql("update AdminInfo set admin_pifu='skins\\wmpx-XMP2.skn' where admin_name='" + us_name + "'");
        }

        private void waiguan2_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (waiguan2_ToolStripMenuItem.Checked == true)
            {
                waiguan1_ToolStripMenuItem.Checked = false;
                waiguan3_ToolStripMenuItem.Checked = false;
            }
            skinUI1.LoadSkinFile("skins\\Watercolor-OLIVE.skn");
            SqlHelper.ExcuteSql("update AdminInfo set admin_pifu='skins\\Watercolor-OLIVE.skn' where admin_name='" + us_name + "'");
        }

        private void waiguan3_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (waiguan3_ToolStripMenuItem.Checked == true)
            {
                waiguan2_ToolStripMenuItem.Checked = false;
                waiguan1_ToolStripMenuItem.Checked = false;
            }
            skinUI1.LoadSkinFile("skins\\ClearLook-SYSTEM.skn");
            SqlHelper.ExcuteSql("update AdminInfo set admin_pifu='skins\\ClearLook-SYSTEM.skn' where admin_name='" + us_name + "'");
        }
        #endregion

        #region 退出程序 关闭窗体
        private void Main_Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否退出系统！", "提示！", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                Application.Exit();
            }
            if (dr == DialogResult.Cancel)
            {
                e.Cancel = true;
            }
        }
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        #endregion
        #endregion

        #region 快捷按钮 窗体控制
        //展开
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (this.tabControl1.SelectedIndex == 0)
            {
                this.treeView1.ExpandAll();//展开所有子节点
                treeView1.SelectedNode = treeView1.Nodes[0]; //重新定位节点
            }
            if (this.tabControl1.SelectedIndex == 2)
            {
                this.treeView2.ExpandAll();//展开所有子节点
                treeView2.SelectedNode = treeView2.Nodes[0]; //重新定位节点
            }
        }
        //折叠
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            if (this.tabControl1.SelectedIndex == 0)
            {
                this.treeView1.CollapseAll();
            }
            if (this.tabControl1.SelectedIndex == 2)
            {
                this.treeView2.CollapseAll();
                foreach (TreeNode CNode in this.treeView2.Nodes)
                {
                    if (CNode.Text == "直辖市")
                    {
                        CNode.Expand();
                        return;
                    }
                }
            }
        }
        //车辆列表
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            if (this.splitContainer1.Panel1Collapsed == false)
            {
                this.splitContainer1.Panel1Collapsed = true;
            }
            else
            {
                this.splitContainer1.Panel1Collapsed = false;
            }
        }
        //实时信息
        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            if (this.splitContainer2.Panel2Collapsed == false)
            {
                this.splitContainer2.Panel2Collapsed = true;
            }
            else
            {
                this.splitContainer2.Panel2Collapsed = false;
            }
        }
        #endregion

        #region 快捷键 地图控制
        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            webBrowserMap.Document.InvokeScript("abc_manyou");//漫游
        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            webBrowserMap.Document.InvokeScript("abc_lk_fd"); //放大
        }

        private void toolStripButton10_Click(object sender, EventArgs e)
        {
            webBrowserMap.Document.InvokeScript("abc_lk_sx"); //缩小
        }

        private void toolStripButton11_Click(object sender, EventArgs e)
        {
            webBrowserMap.Document.InvokeScript("abc_ceju"); //测距
        }

        private void toolStripButton12_Click(object sender, EventArgs e)
        {
            webBrowserMap.Document.InvokeScript("abc_qcceju"); //清除
        }
        #endregion

        #region 快捷键 导出图片

        string saveFile_ImageUrl = "";
        private void toolStripButton13_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = @"jpg|*.jpg|bmp|*.bmp|gif|*.gif";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录 
            saveFileDialog.Title = "导出地图图片";
            DateTime now = DateTime.Now;
            saveFileDialog.FileName = now.Year.ToString().PadLeft(2) + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + "-" + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0');

            //点了保存按钮进入   
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (saveFileDialog.FileName.Trim() == "")
                {
                    MessageBox.Show("请输入要保存的文件名", "提示");
                    return;
                }   
                saveFile_ImageUrl = saveFileDialog.FileName;
                this.timer3.Enabled = true;
            }
            
        }
        //保存当前地图图片
        private void timer3_Tick(object sender, EventArgs e)
        { 
            this.timer3.Enabled = false;
            Bitmap myImage = new Bitmap(this.webBrowserMap.Size.Width, this.webBrowserMap.Size.Height);
            Graphics g = Graphics.FromImage(myImage);
            g.CopyFromScreen(new Point(webBrowserMap.PointToScreen(new Point()).X, webBrowserMap.PointToScreen(new Point()).Y), new Point(0, 0), new Size(this.webBrowserMap.Size.Width, this.webBrowserMap.Size.Height));
            IntPtr dc1 = g.GetHdc();
            g.ReleaseHdc(dc1);
            myImage.Save(saveFile_ImageUrl);    
        }
        #endregion

        #region 快捷键 监控窗口
        //新增监控窗口

        int webbrowIndex = 1;
        private void toolStripButton14_Click(object sender, EventArgs e)
        {
            int tabcont_count = tabControl2.TabCount;
            if (tabcont_count <= 9)
            {
                //tabControl2.TabPages.Add("监控地图");

                TabPage newtab = new TabPage();
                newtab.Text = "监控地图";
                
                WebBrowser wb = new WebBrowser();
                wb.Name = "webBrowserMap" + webbrowIndex;

                //Thread myThread = new Thread(Loadmaps);
                //object ob = wb;
                //myThread.Start(ob);

                string url = Application.StartupPath + "/HTMLPage1.htm";
                wb.Navigate(url);
                wb.ObjectForScripting = this;

                wb.Dock = DockStyle.Fill;
                newtab.Controls.Add(wb);
                tabControl2.TabPages.Add(newtab);
                newtab.Name = wb.Name;
                tabControl2.SelectedIndex = tabControl2.TabPages.Count - 1;//锁定当前选项卡
                //wb.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.Map_DocumentCompleted);
                JiaZai_set_cent(wb);
                JiaZai_Biaozhu(wb);
                //this.timer4.Enabled = true;
            }
            webbrowIndex++;
        }        
        
        private void timer4_Tick(object sender, EventArgs e)
        {
            this.timer4.Enabled = false;
            
        }
        //删除监控窗口
        private void toolStripButton15_Click(object sender, EventArgs e)
        {
            int tabcont_count=tabControl2.TabCount; //获取当前tabControl2的tab 总数 (如果当前选中的是第一页就从最后个删起,否则删除当前选中的)
            if (tabcont_count == 1)
            {
                return;
            }
            else if (tabControl2.SelectedIndex !=0 )
            {
                tabControl2.TabPages.RemoveAt(tabControl2.SelectedIndex);
            }
            else
            {
                tabControl2.TabPages.RemoveAt(tabcont_count - 1);
            }

            
        }
        #endregion

        #region 快捷键 搜索
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            string sousStr=this.toolStripButton2.Text.ToString();
            if (sousStr.Trim()=="")
            {
                return;
            }
            string sousType = this.toolStripButton1.Text.Trim();
            switch (sousType)
            {
                case "搜车牌":
                    SouSuo_Type ar = new SouSuo_Type(1, sousuoTable, sousStr);
                    int x = this.tabControl1.PointToScreen(new Point()).X;
                    int y = this.tabControl1.PointToScreen(new Point()).Y;
                    ar.Location = new Point(x, y); ;
                    ar.Size = new Size(this.tabControl1.Size.Width, this.tabControl1.Size.Height - 5);
                    ar.ShowDialog();
                    break;
                case "搜编号":
                    SouSuo_Type ar1 = new SouSuo_Type(2, sousuoTable, sousStr);
                    int x1 = this.tabControl1.PointToScreen(new Point()).X;
                    int y1 = this.tabControl1.PointToScreen(new Point()).Y;
                    ar1.Location = new Point(x1, y1); ;
                    ar1.Size = new Size(this.tabControl1.Size.Width, this.tabControl1.Size.Height - 5);
                    ar1.ShowDialog();
                    break;
                case "搜目录":
                    string sousuoStr = this.toolStripButton2.Text.Trim();
                    TreeNodeCollection tc = treeView1.Nodes;
                    FindUser(tc, sousuoStr);          
                    break;
                default:
                    break;
            }
 
        }

        private void FindUser(TreeNodeCollection tc, string username)
        {
            foreach (TreeNode Node in tc)
            {
                if (Node.Text.IndexOf(username) >= 0)
                {
                    this.treeView1.SelectedNode = Node;
                }
                FindUser(Node.Nodes, username);
            }
        }


        #endregion

        #region 帮助
        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            guanYu gy = new guanYu();
            gy.Show();
        }
        
        private void 使用帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://help.gps24h.com/Default.aspx?Page=mbus&AspxAutoDetectCookieSupport=1"); 
            

            //Socket_TCp chaosu = new Socket_TCp();
            //int x = webBrowserMap.PointToScreen(new Point()).X - 5;
            //int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
            //chaosu.Location = new Point(x, y);
            //int height = webBrowserMap.Size.Height;
            //chaosu.Size = new Size(webBrowserMap.Size.Width + 10, webBrowserMap.Size.Height);
            //chaosu.Show();

        }
        #endregion

        #region 地图菜单
        private void 添加标注ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //webBrowserMap.Document.InvokeScript("addevent_biaozhu");   
            webBrowserMap.Document.InvokeScript("go_addmarker", new object[] { 30.590637, 114.312744, "你妹", "你妹", true, "你妹", "你妹", "你妹", "你妹", "你妹" });
        }

        private void 管理标注ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DiTu_module.BiaoZhu_Gl bzgl = new XF_GPS.DiTu_module.BiaoZhu_Gl();
            int x = webBrowserMap.PointToScreen(new Point()).X - 5;
            int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
            bzgl.Location = new Point(x, y);
            int height = webBrowserMap.Size.Height;
            bzgl.Size = new Size(496, webBrowserMap.Size.Height);
            bzgl.ShowDialog(this);
        }

        int bz_size; //字体大小
        bool bz_bold; //字体是否粗体
        bool bz_border;//字体是否有边框
        string bz_name="";//标注文字
        string bz_tup=""; //标注图片地址
        string marek_id = "";

        //地图上添加标注
        public void setabc_bq_biaozhu(int fontsize,bool fontbold,bool fontborder,string biaozhu_name,string biaozhu_tup,string tuprul)
        {
            try
            {
                bz_size = fontsize;
                bz_bold = fontbold;
                bz_border = fontborder;
                bz_name = biaozhu_name;
                bz_tup = tuprul;
                string iddate=DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                marek_id = "Markerid" + iddate;
                object getlnglat = webBrowserMap.Document.InvokeScript("abc_bq_biaozhu", new object[] { fontsize, true, fontbold, fontborder, biaozhu_name, biaozhu_tup, marek_id });
            }
            catch
            {
                MessageBox.Show("    添加标注失败！","提示");
            }
        }

        //删除地图上的标注
        public void delete_ditu_bz(string di_markid)
        {
            webBrowserMap.Document.InvokeScript("abc_removeOverlay", new object[] { di_markid });
        }

        //打开标注样式窗体
        public void showaddbiaozhu()
        {
            DiTu_module.AddBiaoZhu addbiaozhu = new XF_GPS.DiTu_module.AddBiaoZhu();
            addbiaozhu.ShowDialog(this);
        }
        //保存标注信息到数据库
        public void insertbiaozhu(object getlnglat)
        {
            string lnglat = getlnglat.ToString();//坐标数组
            string lngx1 = lnglat.Substring(0, lnglat.IndexOf(','));
            string laty1 = lnglat.Substring(lnglat.IndexOf(',') + 1);
            SqlHelper.ExcuteSql("insert into DiTu_BiaoZhu(bz_name,bz_jindu,bz_weidu,bz_Image,bz_fontsize,bz_bold,bz_border,mark_id) values('" + bz_name + "','" + lngx1 + "','" + laty1 + "','" + bz_tup + "'," + bz_size + ",'" + bz_bold + "','" + bz_border + "','"+marek_id+"')");
        }

        //地图定位 
        public void setcent_point(string lngX,string lngY)
        {
            webBrowserMap.Document.InvokeScript("abc_setcenter", new object[] { lngX, lngY });
        }
       

        private void 当前位置设为默认地图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            object getreturn = webBrowserMap.Document.InvokeScript("get_abc_cent");
            
            string lnglat = getreturn.ToString();//坐标数组
            string[] sArray = lnglat.Split(',');

            string lngx1 = sArray[0].ToString();
            string laty1 = sArray[1].ToString();
            string zooml = sArray[2].ToString();
            try
            {
                DataTable dt = SqlHelper.Sqlselect("select * from DiTu_ChongDW where userName='" + us_name + "'", "DiTu_ChongDW");
                if (dt.Rows.Count > 0)
                {
                    SqlHelper.ExcuteSql("update DiTu_ChongDW set cdw_lngx='" + lngx1 + "',cdw_laty='" + laty1 + "',cdw_zooml=" + zooml + " where userName='" + us_name + "'");
                }
                else
                {
                    SqlHelper.ExcuteSql("insert into DiTu_ChongDW(cdw_lngx,cdw_laty,cdw_zooml,userName) values('" + lngx1 + "','" + laty1 + "'," + zooml + ",'" + us_name + "')");
                }
            }
            catch { }
            MessageBox.Show("设置成功","提示");
        }


        public void 移动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowserMap.Document.InvokeScript("abc_manyou");//漫游
        }

        private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowserMap.Document.InvokeScript("abc_lk_sx"); //缩小
        }

        private void 放大ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowserMap.Document.InvokeScript("abc_lk_fd"); //放大
        }

        private void 测距ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowserMap.Document.InvokeScript("abc_ceju"); //测距
        }

        private void 清除画线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowserMap.Document.InvokeScript("abc_qcceju"); //清除
        }

        private void 导出图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripButton13_Click(null, null);
        }

        private void 新建窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 关闭当前窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 本地地图路径设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DiTu_module.Add_gst_Map addgstm = new XF_GPS.DiTu_module.Add_gst_Map();
            addgstm.ShowDialog();
        }

        #endregion

        #region tabControl1
        //双击节点快速定位
        private void treeView2_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
           string clicktxt= e.Node.Text.Trim().ToString();
           if (clicktxt.Contains("省")||clicktxt == "直辖市")
           {
               return;
           }
           else
           {
               webBrowserMap.Document.InvokeScript("setCenter", new object[] { clicktxt });
           }
        }
        //treeView1节点双击监控        
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.webBrowserMap.Document.InvokeScript("abc_removePolyline");
            this.webBrowserMap.Document.InvokeScript("abc_removemarkc");
            this.timer5.Enabled = false;
            this.panel6.Visible = false;

            string TagPID = "";
            try
            {
                TagPID = dataGridView1["c_pid", e.RowIndex].Value.ToString();
                this.panel3.Visible = true;

                Getjkssxx(TagPID);
                cheljkong.Enabled = true;
                TagPIDjk = TagPID;
            }
            catch { this.panel3.Visible = false; }
        }
        string TagPIDjk = "";
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            this.webBrowserMap.Document.InvokeScript("abc_removePolyline");
            string TagPID = "";
            try
            {
                TagPID = int.Parse(e.Node.Tag.ToString()).ToString();
                this.panel3.Visible = true;
                Getjkssxx(TagPID);
                cheljkong.Enabled = true;
                TagPIDjk = TagPID;
            }
            catch { this.panel3.Visible = false; }
        }

        //string gxshijchek = "";//更新时间
        public void Getjkssxx(object tagpid)
        {
            string pid = ""; //车牌号码 label2
            string tel = "";//电话 label8
            string siji = "";//司机 label22
            string chezhu = "";//车主 label10
            string gongsi = "";//公司 label12
            string chexing = "";//车型 label14
            DataTable ssxxidt = SqlHelper.Sqlselect("select * from COMPANY INNER JOIN GROUPS ON COMPANY.cid = GROUPS.cid INNER JOIN PERSONINFO ON PERSONINFO.cpid = GROUPS.gid INNER JOIN CARTYPE on CARTYPE.CID=PERSONINFO.CID where PERSONINFO.PID = " + int.Parse(tagpid.ToString()) + "", "cpgdatatable");
            if (ssxxidt.Rows.Count > 0)
            {
                pid = ssxxidt.Rows[0]["CARCODE"].ToString();
                tel = ssxxidt.Rows[0]["SIMCODE"].ToString();
                siji = ssxxidt.Rows[0]["PNAME"].ToString();
                chezhu = ssxxidt.Rows[0]["GNAME"].ToString();
                gongsi = ssxxidt.Rows[0]["CNAME"].ToString();
                chexing = ssxxidt.Rows[0]["CNAME1"].ToString();

                this.label2.Text = pid;
                this.label8.Text = tel;
                this.label22.Text = siji;
                this.label10.Text = chezhu;
                this.label12.Text = gongsi;
                this.label14.Text = chexing;
            }

            string TagPID = tagpid.ToString();
            Socket_Class sockp = new Socket_Class();
            try
            {
                //DateTime datimed=DateTime.Parse(DateTime.Now.Date.ToString("yyyy-MM-dd") + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute);
                string Carzt = ""; //状态
                string erstate = "";//状态
                string vstate = ""; //状态
                //string ztshij ="";//状态时长 label30
                string speed = "";//速度  label4
                string zhuantai = "";//状态 label6
                string oil = "";   //油量 label18
                string dir = "";   //方向 label16
                string lng = "";
                string lat = "";
                string lnglat = ""; //经纬度 label28
                string ctime = ""; //时间 label20
                string xxdizhi = ""; //详细地址 label26
                string xianlu = ""; //线路 label24

                
                try
                {
                    string che_pid1 = sockp.socket_connection(TagPID);
                    che_pid1 = che_pid1.Substring(2, che_pid1.Length - 4);
                    string[] sArray = che_pid1.Split(',');

                    for (int i = 0; i < sArray.Length; i++)
                    {
                        if (sArray[i].ToString().IndexOf("erstate") == 0)
                        {
                            erstate = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                        }
                        if (sArray[i].ToString().IndexOf("vstate") == 0)
                        {
                            vstate = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            vstate = vstate.Substring(22, 1);
                        }
                        if (sArray[i].ToString().IndexOf("ctime") == 0)
                        {
                            ctime = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            ctime = ctime.Substring(1, ctime.Length - 2);
                        }
                        if (sArray[i].ToString().IndexOf("oil") == 0)
                        {
                            oil = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            oil = oil.Substring(1, oil.Length - 2);
                        }
                        if (sArray[i].ToString().IndexOf("speed") == 0)
                        {
                            speed = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            speed = speed.Substring(1, speed.Length - 2);
                        }
                        if (sArray[i].ToString().IndexOf("dir") == 0)
                        {
                            dir = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            dir = dir.Substring(1, dir.Length - 2);
                        }
                        if (sArray[i].ToString().IndexOf("lng") == 0)
                        {
                            lng = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            lng = lng.Substring(1, lng.Length - 2);
                        }
                        if (sArray[i].ToString().IndexOf("lat") == 0)
                        {
                            lat = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            lat = lat.Substring(1, lat.Length - 2);
                            lat = (double.Parse(lat) + double.Parse("0.00082")).ToString();
                            lng = (double.Parse(lng) + double.Parse("0.006301")).ToString();
                        }
                        if (sArray[i].ToString().IndexOf("of_g_lng") == 0)
                        {
                            //mapabc_pyl = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            //mapabc_pyl = mapabc_pyl.Substring(1, mapabc_pyl.Length - 2);
                            //lng = (double.Parse(lng) + double.Parse(mapabc_pyl)).ToString();
                            
                        }
                    }

                }
                catch
                {
                    Carzt = "2";
                }
                if (erstate != "")
                {
                    DataTable dta = SqlHelper.Sqlselect("select * from QDQT_ERTYPE where QTID=" + erstate + "", "QDQT_ERTYPE");
                    if (dta.Rows.Count > 0) { zhuantai = dta.Rows[0]["TITLE"].ToString(); }
                }
                else if (vstate == "1") { if (float.Parse(speed) > 0) { zhuantai = "行驶"; } else { zhuantai = "停车"; } }
                else if (vstate == "0") { zhuantai = "熄火"; }
                else { zhuantai = "--"; }
                if (lng == ""||lat=="") 
                { 
                    lnglat = "--"; 
                } 
                else 
                { 
                    lnglat = lng + "," + lat;

                    if (lng.Length < 10 && lng.Length > 5)
                    {
                        lng += "0000000";
                    }
                    if (lat.Length < 10 && lat.Length > 5)
                    {
                        lat += "0000000";
                    }
                    string jdlng = lng.Substring(0, lng.IndexOf('.'));
                    string jdlngs = lng.Substring(lng.IndexOf('.') + 1);
                    jdlngs = jdlng + jdlngs.Substring(0, 5);

                    string wdlng = lat.Substring(0, lat.IndexOf('.'));
                    string wdlngs = lat.Substring(lat.IndexOf('.') + 1);
                    wdlngs = wdlng + wdlngs.Substring(0, 5);
                    try
                    {
                        webBrowserMap.Document.InvokeScript("get_mapCsheng", new object[] { jdlngs, wdlngs });
                        object objd = webBrowserMap.Document.InvokeScript("get_mapCsdilixx");
                        xxdizhi = objd.ToString();//返回的坐标地理位置
                    }
                    catch { xxdizhi = ""; }
                }
                if (speed == "") { speed = "--"; } else { speed += " km/h"; }
                if (oil == "") { oil = "--"; }  else{ if(Convert.ToInt32(oil) < 0) { oil = "不支持"; }}
                if (dir == "") { dir = "--"; } else {Chelianfx chfx = new Chelianfx();dir = chfx.Direction(dir); }
                if (lnglat == "") { lnglat = "--"; }
                if (ctime == "") { ctime = "--"; }
                if (xxdizhi == "") { xxdizhi = "--"; }
                if (Carzt == "2") { xianlu = "--"; } else { xianlu = "暂无"; }
                this.label4.Text = speed;
                this.label6.Text = zhuantai;
                this.label18.Text = oil;
                this.label16.Text = dir;
                this.label28.Text = lnglat;
                this.label20.Text = ctime;
                this.label26.Text = xxdizhi;
                this.label24.Text = xianlu;

                string chelfxtu = "";
                if (lnglat != "--")// && dir != "--")
                {
                    if (dir == "--") { dir = "正北"; }
                    if (zhuantai == "行驶") 
                    { 
                        chelfxtu = "绿" + dir + ".png";
                    }
                    else if (zhuantai == "停车") { chelfxtu = "蓝" + dir + ".png"; }
                    else if (zhuantai == "熄火") { chelfxtu = "红" + dir + ".png"; }
                    else { chelfxtu = "灰" + dir + ".png"; }
                    
                    this.webBrowserMap.Document.InvokeScript("abc_p_biaozhu", new object[] { Application.StartupPath + "/Image/车辆图/" + chelfxtu + "", lng, lat, TagPID + "CheGjID" });
                    this.webBrowserMap.Document.InvokeScript("addMPolyline", new object[] { lng, lat, TagPID + "CheGJxian" });
                    //try
                    //{
                    //    string xitongdate = DateTime.Now.Date.ToString("yyyy-MM-dd") + " " + ctime;
                    //    if (gxshijchek == "")
                    //    {
                    //        //SqlHelper.ExcuteSql("insert into CAR_HISTORY(PID,CAR_SUDU,CAR_ZHUANGTAI,CAR_YOULIANG,CAR_FANGXIANG,CAR_GXSHIJIAN,CAR_WEIZHI,CAR_LNGLAT) values(" + int.Parse(tagpid.ToString()) + ",'" + speed + "','" + zhuantai + "','" + oil + "','" + dir + "','" + xitongdate + "','" + xxdizhi + "','" + lnglat + "')");
                    //    }
                    //    else
                    //    {
                    //        if (DateTime.Parse(gxshijchek) != DateTime.Parse(xitongdate))
                    //        {
                    //            //SqlHelper.ExcuteSql("insert into CAR_HISTORY(PID,CAR_SUDU,CAR_ZHUANGTAI,CAR_YOULIANG,CAR_FANGXIANG,CAR_GXSHIJIAN,CAR_WEIZHI,CAR_LNGLAT) values(" + int.Parse(tagpid.ToString()) + ",'" + speed + "','" + zhuantai + "','" + oil + "','" + dir + "','" + xitongdate + "','" + xxdizhi + "','" + lnglat + "')");
                    //        }
                    //    }
                    //    gxshijchek = xitongdate;
                    //}
                    //catch { MessageBox.Show("保存信息失败"); }
                }
                else 
                {
                    this.webBrowserMap.Document.InvokeScript("abc_removemarkc");
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); }
        }  

        private void 取消监控ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.panel3.Visible = false;
            cheljkong.Enabled = false;
            this.webBrowserMap.Document.InvokeScript("abc_removemarkc");
            this.webBrowserMap.Document.InvokeScript("abc_removePolyline");
        }

        //隐藏车辆监控实时信息界面
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.panel3.Visible = false;
            cheljkong.Enabled = false;
            this.webBrowserMap.Document.InvokeScript("abc_removemarkc");
            this.webBrowserMap.Document.InvokeScript("abc_removePolyline");
        }
        private void cheljkong_Tick(object sender, EventArgs e)
        {
            Thread myThread = new Thread(jkongcar);
            myThread.Start();
        }
        public void jkongcar()
        {
            SetTextBoxjk(TagPIDjk);
        }

        private delegate void SetTextBoxCallbackjk(string str);
        private SetTextBoxCallbackjk setTextBoxCallbackjk;

        private void SetTextBoxjk(string str)
        {
            if (this.label4.InvokeRequired == true)
            {
                this.Invoke(setTextBoxCallbackjk, str);
            }
            else
            {
                Getjkssxx(TagPIDjk);
            }
        }

        #endregion

        #region 监控报表
        private void 报警记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            JK_BaoBiao.Alarm_record ar = new XF_GPS.JK_BaoBiao.Alarm_record(sousuoTable);
            int x = webBrowserMap.PointToScreen(new Point()).X - 5;
            int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
            ar.Location = new Point(x, y);
            int height = webBrowserMap.Size.Height;
            ar.Size = new Size(webBrowserMap.Size.Width+5, webBrowserMap.Size.Height);
            ar.ShowDialog();
        }

        private void 持续超速报表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            JK_BaoBiao.ChiXu_ChaoSu chaosu = new XF_GPS.JK_BaoBiao.ChiXu_ChaoSu(sousuoTable);
            int x = webBrowserMap.PointToScreen(new Point()).X - 5;
            int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
            chaosu.Location = new Point(x, y);
            int height = webBrowserMap.Size.Height;
            chaosu.Size = new Size(webBrowserMap.Size.Width+5, webBrowserMap.Size.Height);
            chaosu.ShowDialog();

        } 

        private void 车辆分布报表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            JK_BaoBiao.CheLiang_Fenbu cf = new XF_GPS.JK_BaoBiao.CheLiang_Fenbu();
            int x = webBrowserMap.PointToScreen(new Point()).X - 5;
            int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
            cf.Location = new Point(x, y);
            int height = webBrowserMap.Size.Height;
            cf.Size = new Size(webBrowserMap.Size.Width+5, webBrowserMap.Size.Height);
            cf.ShowDialog();
        }

        private void 车辆当前状态报表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            JK_BaoBiao.CheLiang_DQzhuangtai cf = new XF_GPS.JK_BaoBiao.CheLiang_DQzhuangtai(sousuoTable,webBrowserMap);
            int x = webBrowserMap.PointToScreen(new Point()).X - 5;
            int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
            cf.Location = new Point(x, y);
            int height = webBrowserMap.Size.Height;
            cf.Size = new Size(webBrowserMap.Size.Width + 5, webBrowserMap.Size.Height);
            cf.ShowDialog();
        }

        private void 车辆行驶状态报表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            JK_BaoBiao.CheLiang_XSzhuangtai cf = new XF_GPS.JK_BaoBiao.CheLiang_XSzhuangtai(sousuoTable, webBrowserMap);
            int x = webBrowserMap.PointToScreen(new Point()).X - 5;
            int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
            cf.Location = new Point(x, y);
            int height = webBrowserMap.Size.Height;
            cf.Size = new Size(webBrowserMap.Size.Width + 5, webBrowserMap.Size.Height);
            cf.ShowDialog();
        }

        private void 车辆里程统计日报表ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 车辆里程统计月报表ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 拍照报表ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 进出站点报表ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        private void 车辆行驶记录报表ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region 加载车辆

        private delegate void SetTextBoxCallback(string str);
        private SetTextBoxCallback setTextBoxCallback;

        private void SetTextBox(string str)
        {
            if (this.treeView1.InvokeRequired == true)
            {
                this.Invoke(setTextBoxCallback, str);
            }
            else
            {
                try
                {
                    DataTable dt = sousuoTable;
                    if (dt.Rows.Count > 0)
                    {
                        addTree(dt, dt.Rows[0]["CPID"].ToString(), null);
                    }
                }
                catch { }
                //this.Invoke((Action)addContrls);

            }
        }

        private delegate void SetTextBoxCallback1(TreeNode Str);
        private SetTextBoxCallback1 setTextBoxCallback1;
        DataTable dtssxx = new DataTable();
        private void SetTextBox1(TreeNode noder)
        {
            if (this.treeView1.InvokeRequired == true)
            {
                this.Invoke(setTextBoxCallback1, noder);
            }
            else
            {
                jiazaizhuangtai(noder);
            }
        }

        public void jiazaizhuangtai(TreeNode noder)
        {
            try
            {
                string Carzt = ""; //状态
                string erstate = "";//状态
                string vstate = ""; //状态

                //------------------------------------
                string pid = noder.Text; //车牌号码
                string zhuantai = "";//状态
                string speed = "";//速度
                string oil = "";   //油量
                string dir = "";   //方向
                string lnglat = ""; //经纬度
                string ctime = ""; //时间
                string xxdizhi = ""; //详细地址

                string jindulng = "";
                string weidulat = "";
                try
                {
                    string che_pid1 = socke.socket_connection(noder.Tag.ToString());
                    //MessageBox.Show(che_pid1);
                    che_pid1 = che_pid1.Substring(2, che_pid1.Length - 4);
                    string[] sArray = che_pid1.Split(',');

                    for (int i = 0; i < sArray.Length; i++)
                    {
                        if (sArray[i].ToString().IndexOf("erstate") == 0)
                        {
                            erstate = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);

                        }
                        if (sArray[i].ToString().IndexOf("vstate") == 0)
                        {
                            vstate = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            vstate = vstate.Substring(22, 1);
                        }
                        if (sArray[i].ToString().IndexOf("ctime") == 0)
                        {
                            ctime = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            ctime = ctime.Substring(1, ctime.Length - 2);
                        }
                        if (sArray[i].ToString().IndexOf("oil") == 0)
                        {
                            oil = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            oil = oil.Substring(1, oil.Length - 2);
                        }
                        //--------------------------------------------------------------------------------------

                        if (sArray[i].ToString().IndexOf("speed") == 0)
                        {
                            speed = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            speed = speed.Substring(1, speed.Length - 2);
                        }
                        if (sArray[i].ToString().IndexOf("dir") == 0)
                        {
                            dir = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            dir = dir.Substring(1, dir.Length - 2);
                        }
                        if (sArray[i].ToString().IndexOf("lng") == 0)
                        {
                            lnglat = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            lnglat = lnglat.Substring(1, lnglat.Length - 2);
                            jindulng = lnglat;
                        }
                        if (sArray[i].ToString().IndexOf("lat") == 0)
                        {
                            string lat = sArray[i].ToString().Substring(sArray[i].ToString().IndexOf(':') + 1);
                            weidulat = lat.Substring(1, lat.Length - 2);
                            lnglat += "," + weidulat;

                        }
                    }

                }
                catch
                {
                    Carzt = "2";
                }

                if (Carzt != "" || erstate != "" || vstate != "" || ctime != "")
                {
                    if (Carzt == "2")
                    {
                        noder.Text = noder.Text;
                        noder.ForeColor = System.Drawing.Color.Gray;
                        noder.SelectedImageIndex = 8;
                        noder.ImageIndex = 8;
                    }
                    else if (erstate != "")
                    {
                        DataTable dta = SqlHelper.Sqlselect("select * from QDQT_ERTYPE where QTID=" + erstate + "", "QDQT_ERTYPE");
                        if (dta.Rows.Count > 0)
                        {
                            noder.Text = noder.Text + "(" + dta.Rows[0]["TITLE"].ToString() + ")";
                            noder.ForeColor = System.Drawing.Color.Gray;
                            noder.SelectedImageIndex = 8;
                            noder.ImageIndex = 8;
                            zhuantai = dta.Rows[0]["TITLE"].ToString();
                        }
                    }
                    else if (vstate == "1")
                    {
                        string CarzType = "";
                        if (float.Parse(speed) > 0)
                        {

                            CarzType = "行驶";
                            noder.ForeColor = System.Drawing.Color.Green;
                            noder.SelectedImageIndex = 9;
                            noder.ImageIndex = 9;
                        }
                        else
                        {
                            CarzType = "停车";
                            noder.ForeColor = System.Drawing.Color.LightSeaGreen;
                            noder.SelectedImageIndex = 3;
                            noder.ImageIndex = 3;
                        }
                        zhuantai = CarzType;
                        noder.Text = noder.Text + "(" + CarzType + ")";
                    }
                    else if (vstate == "0")
                    {
                        noder.Text = noder.Text + "(熄火)";
                        noder.ForeColor = System.Drawing.Color.Red;
                        noder.SelectedImageIndex = 5;
                        noder.ImageIndex = 5;
                        zhuantai = "熄火";
                    }
                    else { noder.Text = noder.Text; }

                }

                DataRow dr3 = dtssxx.NewRow();
                dr3["cphm"] = pid;
                if (zhuantai == "") { dr3["zt"] = "--"; } else { dr3["zt"] = zhuantai; }
                if (speed == "") { dr3["sd"] = "--"; } else { dr3["sd"] = speed; }
                if (oil == "") { dr3["yl"] = "--"; } else { if (Convert.ToInt32(oil) < 0) { dr3["yl"] = "不支持"; } else { dr3["yl"] = oil; } }
                if (dir == "") { dr3["fx"] = "--"; }
                else
                {
                    Chelianfx chfx = new Chelianfx();
                    string fx = chfx.Direction(dir);
                    dr3["fx"] = fx;
                }
                if (lnglat == "")
                { dr3["jwd"] = "--"; dr3["xxdz"] = "--"; }
                else
                {
                    dr3["jwd"] = lnglat;
                    try
                    {
                        if (jindulng.Length < 10 && jindulng.Length > 5)
                        {
                            jindulng += "0000000";
                        }
                        if (weidulat.Length < 10 && weidulat.Length > 5)
                        {
                            weidulat += "0000000";
                        }
                        string jdlng = jindulng.Substring(0, jindulng.IndexOf('.'));
                        string jdlngs = jindulng.Substring(jindulng.IndexOf('.') + 1);
                        jdlngs = jdlng + jdlngs.Substring(0, 5);

                        string wdlng = weidulat.Substring(0, weidulat.IndexOf('.'));
                        string wdlngs = weidulat.Substring(weidulat.IndexOf('.') + 1);
                        wdlngs = wdlng + wdlngs.Substring(0, 5);

                        webBrowserMap.Document.InvokeScript("get_mapCsheng", new object[] { jdlngs, wdlngs });
                        object objd = webBrowserMap.Document.InvokeScript("get_mapCsdilixx");
                        xxdizhi = objd.ToString();//返回的坐标地理位置
                        dr3["xxdz"] = xxdizhi;
                    }
                    catch
                    { dr3["xxdz"] = "--"; }

                }
                if (ctime == "") { dr3["gxsj"] = "--"; } else { dr3["gxsj"] = ctime; }
                dr3["cpid"] = noder.Tag.ToString();
                dtssxx.Rows.Add(dr3);
                this.dataGridView1.DataSource = dtssxx;
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); }
        }

        public void Get_organization()
        {
            SetTextBox("aa");
        }

        private void addContrls()
        {
            pictureB1.Visible = false;
            lb1.Visible = false; ;
            this.tabPage1.Controls.Add(this.treeView1);
            this.treeView1.ExpandAll();  //张开所有子节点
            treeView1.SelectedNode = treeView1.Nodes[0]; //重新定位节点
            toolStripStatusLabel2.Text = "车辆数 :  " + checount + " 台  |  ";


            Thread threadReceive1 = new Thread(new ThreadStart(finduse));
            threadReceive1.Start();
        }

        public void finduse()
        {
            if (sousuoTable != null)
            {
                dtssxx.Columns.Add("cphm");
                dtssxx.Columns.Add("zt");
                dtssxx.Columns.Add("sd");
                dtssxx.Columns.Add("yl");
                dtssxx.Columns.Add("fx");
                dtssxx.Columns.Add("jwd");
                dtssxx.Columns.Add("gxsj");
                dtssxx.Columns.Add("xxdz");
                dtssxx.Columns.Add("cpid");
                for (int i = 0; i < sousuoTable.Rows.Count; i++)
                {
                    DataTable dt_cgp = SqlHelper.Sqlselect("select * from COMPANY INNER JOIN GROUPS ON COMPANY.cid = GROUPS.cid INNER JOIN PERSONINFO ON PERSONINFO.cpid = GROUPS.gid where COMPANY.cid=" + int.Parse(sousuoTable.Rows[i]["CId"].ToString()) + "", "CGP_Tabled");
                    if (dt_cgp.Rows.Count > 0)
                    {
                        //ThreadPool.SetMinThreads(10, 10);
                        //ThreadPool.SetMaxThreads(20, 20);

                        for (int j = 0; j < dt_cgp.Rows.Count; j++)
                        {
                            if (int.Parse(dt_cgp.Rows[j]["CID2"].ToString()) > 0 && int.Parse(dt_cgp.Rows[j]["CID2"].ToString()) != 8 && int.Parse(dt_cgp.Rows[j]["CID2"].ToString()) != 10 && int.Parse(dt_cgp.Rows[j]["CID2"].ToString()) != 11)
                            {
                                TreeNodeCollection tc = treeView1.Nodes;
                                FindUser2(tc, int.Parse(dt_cgp.Rows[j]["PID"].ToString()));
                            }

                            //ThreadPool.QueueUserWorkItem(SetTextBox1, Node);
                        }
                    }

                }
            }
        }
        public void FindUser2(TreeNodeCollection tc, int userid)
        {
            foreach (TreeNode Node in tc)
            {
                if (Node.Tag.ToString().IndexOf(userid.ToString()) >= 0)
                {
                  SetTextBox1(Node);
                }
                FindUser2(Node.Nodes, userid);
            }
        }

        int checount = 0;
        Socket_Class socke = new Socket_Class();
        /// <summary>
        /// 递归加载角色树列表
        /// </summary>
        /// <param name="fatherid">节点的fatherid</param>
        /// <param name="pnode">该节点</param>
        public void addTree(DataTable table ,string fatherid, TreeNode pnode)
        {

            DataView dvTree = new DataView(table);
            dvTree.RowFilter = "[CPID] = " + fatherid;
            DataTable dt_cgp1;
            foreach (DataRowView drv in dvTree)
            {
                TreeNode node = new TreeNode();
                if (pnode == null)
                {
                    node.Text = drv["CName"].ToString();
                    node.Tag = drv["CName"].ToString();    //作为节点标识符
                    node.ContextMenuStrip = this.contextMenuStrip1;
                    treeView1.Nodes.Add(node);
                    node.SelectedImageIndex = 0;
                    node.ImageIndex = 0;

                    DataTable dt_cgp = SqlHelper.Sqlselect("select * from COMPANY INNER JOIN GROUPS ON COMPANY.cid = GROUPS.cid INNER JOIN PERSONINFO ON PERSONINFO.cpid = GROUPS.gid where COMPANY.cid=" + int.Parse(drv["CId"].ToString()) + "", "CGP_Table");
                    if (dt_cgp.Rows.Count > 0)
                    {
                        TreeNode noder;
                        for (int r = 0; r < dt_cgp.Rows.Count; r++)
                        {
                            
                            noder = new TreeNode();
                            if (int.Parse(dt_cgp.Rows[r]["CID2"].ToString()) < 1 || int.Parse(dt_cgp.Rows[r]["CID2"].ToString()) == 8 || int.Parse(dt_cgp.Rows[r]["CID2"].ToString()) == 10 || int.Parse(dt_cgp.Rows[r]["CID2"].ToString()) == 11)
                            {
                                noder.SelectedImageIndex = 4;
                                noder.ImageIndex = 4;
                            }
                            else
                            {
                                noder.SelectedImageIndex = 3;
                                noder.ImageIndex = 3;
                            }
                            noder.Text = dt_cgp.Rows[r]["carcode"].ToString();
                            noder.Tag = dt_cgp.Rows[r]["PID"].ToString();
                            noder.ContextMenuStrip = this.contextMenuStrip2;
                            node.Nodes.Add(noder);
                        }

                    }
                    checount += dt_cgp.Rows.Count;
                    addTree(table, drv["CId"].ToString(), node);//递归  该节点加载子节点

                }
                else
                {
                    node.Text = drv["CName"].ToString();
                    node.Tag = drv["CName"].ToString();//作为节点标识符
                    node.SelectedImageIndex = 2;
                    node.ImageIndex = 2;
                    node.ContextMenuStrip = this.contextMenuStrip1;//添加右击菜单
                    pnode.Nodes.Add(node);

                    dt_cgp1 = SqlHelper.Sqlselect("select * from COMPANY INNER JOIN GROUPS ON COMPANY.cid = GROUPS.cid INNER JOIN PERSONINFO ON PERSONINFO.cpid = GROUPS.gid where COMPANY.cid=" + int.Parse(drv["CId"].ToString()) + "", "CGP_Table");
                    if (dt_cgp1.Rows.Count > 0)
                    {
                        TreeNode noder1;
                        for (int r1 = 0; r1 < dt_cgp1.Rows.Count; r1++)// dt_cgp1.Rows.Count
                        {
                            noder1 = new TreeNode();
                            if (int.Parse(dt_cgp1.Rows[r1]["CID2"].ToString()) < 1 || int.Parse(dt_cgp1.Rows[r1]["CID2"].ToString()) == 8 || int.Parse(dt_cgp1.Rows[r1]["CID2"].ToString()) == 10 || int.Parse(dt_cgp1.Rows[r1]["CID2"].ToString()) == 11)
                            {
                                noder1.SelectedImageIndex = 4;
                                noder1.ImageIndex = 4;
                            }
                            else
                            {
                                noder1.SelectedImageIndex = 3;
                                noder1.ImageIndex = 3;
                            }
                            noder1.Text = dt_cgp1.Rows[r1]["carcode"].ToString();
                            noder1.Tag = dt_cgp1.Rows[r1]["PID"].ToString();
                            noder1.ContextMenuStrip = this.contextMenuStrip2;
                            node.Nodes.Add(noder1);
                        }

                    }
                    checount += dt_cgp1.Rows.Count;
                    addTree(table, drv["CId"].ToString(), node);//递归  该节点加载子节点
                }
            }
        }

        private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
        {
            if (e.Node == treeView1.Nodes[0])
            {
                return;
            }
            e.Node.SelectedImageIndex = 1;
            e.Node.ImageIndex = 1;
        }

        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            if (e.Node == treeView1.Nodes[0])
            {
                return;
            }
            e.Node.SelectedImageIndex = 2;
            e.Node.ImageIndex = 2;
        }

        #endregion

        #region treeView1右击菜单

        #region contextMenuStrip1
        //添加实时信息列表-当前级别车辆
        private void dangqianjibiecheliang_Click(object sender, EventArgs e)
        {

        }

        //添加实时信息列表-所有下级车辆
        private void suoyouxiajichel_Click(object sender, EventArgs e)
        {

        }

        //在实时列表中移除-当前级别车辆
        private void dqleibyc_Click(object sender, EventArgs e)
        {

        }
        //在实时列表中移除-所有下级车辆
        private void syouxiayc_Click(object sender, EventArgs e)
        {

        }

        //清空实时信息列表
        private void cliershis_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = null;
        }

        #endregion

        #region contextMenuStrip2
        
        TreeNode CurrentNode=null;
        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)//判断你点的是不是右键   
            {
                Point ClickPoint = new Point(e.X, e.Y);
                CurrentNode = treeView1.GetNodeAt(ClickPoint);
            } 
        }
        //新增监控窗口
        private void newwindowjk_Click(object sender, EventArgs e)
        {
            int tabcont_count = tabControl2.TabCount;
            if (tabcont_count <= 9)
            {
                //tabControl2.TabPages.Add("监控地图");

                TabPage newtab = new TabPage();
                newtab.Text = "监控地图";

                WebBrowser wb = new WebBrowser();
                wb.Name = "webBrowserMap" + webbrowIndex;

                //Thread myThread = new Thread(Loadmaps);
                //object ob = wb;
                //myThread.Start(ob);

                string url = Application.StartupPath + "/HTMLPage1.htm";
                wb.Navigate(url);
                wb.ObjectForScripting = this;

                wb.Dock = DockStyle.Fill;
                newtab.Controls.Add(wb);
                tabControl2.TabPages.Add(newtab);
                newtab.Name = wb.Name;
                tabControl2.SelectedIndex = tabControl2.TabPages.Count - 1;//锁定当前选项卡
                //wb.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.Map_DocumentCompleted);
                //JiaZai_set_cent(wb);
                //JiaZai_Biaozhu(wb);
                //this.timer4.Enabled = true;
            }
            webbrowIndex++;
        }

        //查看卫星地图
        private void lookweixmap_Click(object sender, EventArgs e)
        {
            //this.panel4
            try
            {
                if (CurrentNode != null)
                {
                    string Cname = CurrentNode.Text;
                    string CTag = CurrentNode.Tag.ToString();
                    try
                    {
                        Cname = Cname.Substring(0, Cname.IndexOf('('));
                    }
                    catch { Cname = CurrentNode.Text; }
                    YouJi_Caidan.GoogleMap gmap = new XF_GPS.YouJi_Caidan.GoogleMap(Cname, CTag);
                    int x = webBrowserMap.PointToScreen(new Point()).X - 5;
                    int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
                    gmap.Location = new Point(x, y);
                    int height = webBrowserMap.Size.Height;

                        gmap.Size = new Size(this.panel4.Size.Width - 8, this.panel4.Size.Height-28);

                    gmap.Text = "查看 " + Cname+" 卫星图";
                    gmap.ShowDialog();
                }
            }
            catch { }
        }

        #region 查看历史轨迹
        private void looklishiguiji_Click(object sender, EventArgs e)
        {
            this.webBrowserMap.Document.InvokeScript("abc_removePolyline");
            //取消车辆监控
            this.panel3.Visible = false;
            cheljkong.Enabled = false;
            this.webBrowserMap.Document.InvokeScript("abc_removemarkc");
            this.webBrowserMap.Document.InvokeScript("abc_removePolyline");

            if (CurrentNode != null)
            {
                string Cname = CurrentNode.Text;
                string CTag = CurrentNode.Tag.ToString();
                try
                {
                    Cname = Cname.Substring(0, Cname.IndexOf('('));
                }
                catch { Cname = CurrentNode.Text; }
                //查询轨迹界面
                YouJi_Caidan.CX_lsgj cxls = new XF_GPS.YouJi_Caidan.CX_lsgj(CTag);
                //cxls.Owner = this;
                cxls.Text = "查询 " + Cname + " 历史轨迹";
                cxls.P_F1 = this;
                cxls.ShowDialog();
                ls_guiji = 0;
                this.trackBar1.Value = 0;
            }
            
        }

        
        DataTable DTGjTable = null;
        DataTable DTGjssxxidt = null;
        string Hfdiansu = "";
        string radioButtonName = "";
        string HfSuDu = "";
        int ls_guijiys = 1; //1同时画 2先画轨迹 3后画轨迹
        public void panevisle(DataTable dtgj, DataTable ssxxidt, string Hfsudu, string hfdiansu, string radiobuttonname) //显示历史轨迹信息界面
        {
            ls_guiji = 0;
            ls_nxh = 0;

            this.panel6.Visible = true;
            DTGjTable = dtgj;
            DTGjssxxidt = ssxxidt;
            Hfdiansu = hfdiansu;
            HfSuDu = Hfsudu;
            radioButtonName = radiobuttonname;
            this.comboBox1.Text = Hfsudu;
            if (radioButtonName == "radioButton1")
            {
                ls_guijiys = 1;
            }
            if (radioButtonName == "radioButton2")
            {
                ls_guijiys = 2;
            }
            if (radioButtonName == "radioButton3")
            {
                ls_guijiys = 3;
            }
            string pname = ""; //车牌号码 label58
            string tel = "";//电话 label52
            string siji = "";//司机 label38
            string chezhu = "";//车主 label50
            string gongsi = "";//公司 label48
            string chexing = "";//车型 label46
            pname = ssxxidt.Rows[0]["CARCODE"].ToString();
            tel = ssxxidt.Rows[0]["SIMCODE"].ToString();
            siji = ssxxidt.Rows[0]["PNAME"].ToString();
            chezhu = ssxxidt.Rows[0]["GNAME"].ToString();
            gongsi = ssxxidt.Rows[0]["CNAME"].ToString();
            chexing = ssxxidt.Rows[0]["CNAME1"].ToString();
            this.label58.Text = pname;
            this.label52.Text = tel;
            this.label38.Text = siji;
            this.label50.Text = chezhu;
            this.label48.Text = gongsi;
            this.label46.Text = chexing;

            this.timer5.Interval = Convert.ToInt32(double.Parse(Hfsudu) * 1000);
            this.timer5.Enabled = true;
            this.trackBar1.Maximum = dtgj.Rows.Count;
        }
        int ls_guiji = 0;
        int ls_nxh = 0;
        public void timer5_Tick(object sender, EventArgs e)
        {
            string guijis = "";
            if (ls_guijiys == 1) { guijis = "0"; }
            if (ls_guijiys == 2) { guijis = "1"; } //画线
            if (ls_guijiys == 3) { guijis = "2"; } //画车
            for (int i = 0; i <= int.Parse(Hfdiansu); i++)
            {
                if (ls_guiji < DTGjTable.Rows.Count)
                {
                    HuaLiShiguiji(ls_guiji, guijis);
                    this.trackBar1.Value = ls_guiji + 1;
                }
                ls_guiji++;
            }
            if (ls_guiji >= DTGjTable.Rows.Count)
            {
                if (ls_guijiys == 1)
                {
                    this.timer5.Enabled = false;
                }
                else
                {
                    string guijice = "";
                    if (guijis == "1") { guijice = "2"; }
                    if (guijis == "2") { guijice = "1"; }
                    for (int i = 0; i <= int.Parse(Hfdiansu); i++)
                    {
                        if (ls_nxh < DTGjTable.Rows.Count)
                        {
                            HuaLiShiguiji(ls_nxh, guijice);
                            this.trackBar1.Value = ls_nxh + 1;
                        }
                        ls_nxh++;
                    }
                    if (ls_nxh >= DTGjTable.Rows.Count)
                    {
                        this.timer5.Enabled = false;
                    }
                }
            }

        }

        public void HuaLiShiguiji(int gjrows,string guijixh)
        {
            try
            {
                DataTable dtgj = DTGjTable;

                string xianlu = "";//线路 label36
                string sudu = "";//速度 label56
                string zhuangtai = "";//状态 label54
                string youliang = "";//油量 label42
                string fangx = "";//方向 label44
                string gxshij = "";//更新时间 label40
                string dizhi = "";//地址 label34
                string lnglat = "";//经纬度label32
                string lat = "";
                string lng = "";

                if (dtgj.Rows.Count > 0)
                {
                    sudu = dtgj.Rows[gjrows]["SPEED"].ToString();
                    //zhuangtai = dtgj.Rows[gjrows]["CAR_ZHUANGTAI"].ToString();
                    if (double.Parse(sudu) > 0)
                    { zhuangtai = "行驶"; }
                    else { zhuangtai = "停车"; }
                    youliang = dtgj.Rows[gjrows]["OILSTATE"].ToString();
                    if (double.Parse(youliang) < 0)
                    { youliang = "不支持"; }
                    fangx = dtgj.Rows[gjrows]["DIRECTION"].ToString();
                    Chelianfx chfx = new Chelianfx();
                    fangx = chfx.Direction(fangx);
                    if (fangx == "--") { fangx = "正北"; }
                    gxshij = dtgj.Rows[gjrows]["CTIME"].ToString();

                    //dizhi = dtgj.Rows[gjrows]["CAR_WEIZHI"].ToString();
                    lat = dtgj.Rows[gjrows]["LATITUDE"].ToString();
                    lng = dtgj.Rows[gjrows]["LONGITUDE"].ToString();
                    lat = (double.Parse(lat) + double.Parse("0.00082")).ToString();
                    lng = (double.Parse(lng) + double.Parse("0.006301")).ToString();

                    lnglat = lng + "," + lat;

                    if (lng.Length < 10 && lng.Length > 5)
                    {
                        lng += "0000000";
                    }
                    if (lat.Length < 10 && lat.Length > 5)
                    {
                        lat += "0000000";
                    }
                    string jdlng = lng.Substring(0, lng.IndexOf('.'));
                    string jdlngs = lng.Substring(lng.IndexOf('.') + 1);
                    jdlngs = jdlng + jdlngs.Substring(0, 5);

                    string wdlng = lat.Substring(0, lat.IndexOf('.'));
                    string wdlngs = lat.Substring(lat.IndexOf('.') + 1);
                    wdlngs = wdlng + wdlngs.Substring(0, 5);

                    webBrowserMap.Document.InvokeScript("get_mapCsheng", new object[] { jdlngs, wdlngs });
                    object objd = webBrowserMap.Document.InvokeScript("get_mapCsdilixx");
                    dizhi = objd.ToString();//返回的坐标地理位置


                }
                this.label36.Text = xianlu;
                this.label56.Text = sudu;
                this.label54.Text = zhuangtai;
                this.label42.Text = youliang;
                this.label44.Text = fangx;
                this.label40.Text = gxshij;
                this.label34.Text = dizhi;
                this.label32.Text = lnglat;

                string chelfxtu = "";
                if (lnglat != "")
                {
                    if (zhuangtai == "行驶")
                    {
                        chelfxtu = "绿" + fangx + ".png";
                    }
                    else if (zhuangtai == "停车") { chelfxtu = "蓝" + fangx + ".png"; }
                    else if (zhuangtai == "熄火") { chelfxtu = "红" + fangx + ".png"; }
                    else { chelfxtu = "灰" + fangx + ".png"; }

                    if (guijixh == "0")
                    {
                        this.webBrowserMap.Document.InvokeScript("abc_p_biaozhu", new object[] { Application.StartupPath + "/Image/车辆图/" + chelfxtu + "", lng, lat, "GjID" + lnglat });
                        this.webBrowserMap.Document.InvokeScript("addMPolyline", new object[] { lng, lat, "GJ" + lnglat });
                    }
                    if (guijixh == "1")
                    {
                        this.webBrowserMap.Document.InvokeScript("addMPolyline", new object[] { lng, lat, "GJ" + lnglat });
                    }
                    if (guijixh == "2")
                    {
                        this.webBrowserMap.Document.InvokeScript("abc_p_biaozhu", new object[] { Application.StartupPath + "/Image/车辆图/" + chelfxtu + "", lng, lat, "GjID" + lnglat });
                    }
                }
                else
                {
                    this.webBrowserMap.Document.InvokeScript("abc_removemarkc");
                }
            }
            catch { }
        }
        #endregion
        //读取基本信息
        private void readjibxxi_Click(object sender, EventArgs e)
        {
            try
            {
                if (CurrentNode != null)//判断你点的是不是一个节点   
                {
                    string Cname = CurrentNode.Text;
                    string CTag = CurrentNode.Tag.ToString();
                    try
                    {
                        Cname = Cname.Substring(0, Cname.IndexOf('('));
                    }
                    catch { Cname = CurrentNode.Text; }
                    YouJi_Caidan.Chel_jbxxi yj_cj = new XF_GPS.YouJi_Caidan.Chel_jbxxi(CTag);
                    yj_cj.Text = "读取 " + Cname + " 基本信息";
                    yj_cj.ShowDialog();
                }
            }
            catch { }
        }
        //发短信
        private void sendMess_Click(object sender, EventArgs e)
        {
            try
            {
                if (CurrentNode != null)//判断你点的是不是一个节点   
                {
                    string Cname = CurrentNode.Text;
                    string CTag = CurrentNode.Tag.ToString();
                    try
                    {
                        Cname = Cname.Substring(0, Cname.IndexOf('('));
                    }
                    catch { Cname = CurrentNode.Text; }
                    YouJi_Caidan.SendMISMess yj_cj = new XF_GPS.YouJi_Caidan.SendMISMess(Cname);
                    yj_cj.Text = "发送短信到 " + Cname;
                    yj_cj.ShowDialog();
                }
            }
            catch { }
        }
        
        

        //按时间段查询
        private void anshijiaduanchax_Click(object sender, EventArgs e)
        {
            try
            {
                if (CurrentNode != null)//判断你点的是不是一个节点   
                {
                    string Cname = CurrentNode.Text;
                    string CTag = CurrentNode.Tag.ToString();
                    try
                    {
                        Cname = Cname.Substring(0, Cname.IndexOf('('));
                    }
                    catch { Cname = CurrentNode.Text; }
                    YouJi_Caidan.DateTimeSelect_Lc yj_cj = new XF_GPS.YouJi_Caidan.DateTimeSelect_Lc(Cname, CTag);
                    yj_cj.Text = Cname+" 里程统计";
                    yj_cj.ShowDialog();
                }
            }
            catch { }
        }

        //总里程查询
        private void lichnegtongji_Click(object sender, EventArgs e)
        {
            try
            {
                if (CurrentNode != null)//判断你点的是不是一个节点   
                {
                    string Cname = CurrentNode.Text;
                    string CTag = CurrentNode.Tag.ToString();
                    try
                    {
                        Cname = Cname.Substring(0, Cname.IndexOf('('));
                    }
                    catch { Cname = CurrentNode.Text;}
                    YouJi_Caidan.Select_Lc yj_cj = new XF_GPS.YouJi_Caidan.Select_Lc(Cname, CTag);
                    yj_cj.Text = Cname + " 总里程统计";
                    yj_cj.ShowDialog();
                }
            }
            catch { }
        }
        
        #endregion

        #endregion

        #region 车辆管理
        private void 车辆目录设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Che_Manger.Che_mlsz che_mlsz = new XF_GPS.Che_Manger.Che_mlsz(sousuoTable);
            int x = webBrowserMap.PointToScreen(new Point()).X - 5;
            int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
            che_mlsz.Location = new Point(x, y);
            int height = webBrowserMap.Size.Height;
            che_mlsz.Size = new Size(webBrowserMap.Size.Width+5, webBrowserMap.Size.Height);
            che_mlsz.ShowDialog();
        }

        private void 车辆轨迹样式设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void 车主管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Che_Manger.Che_zhugl zhugl = new XF_GPS.Che_Manger.Che_zhugl();
            int x = webBrowserMap.PointToScreen(new Point()).X - 5;
            int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
            zhugl.Location = new Point(x, y);
            int height = webBrowserMap.Size.Height;
            zhugl.Size = new Size(webBrowserMap.Size.Width+5, webBrowserMap.Size.Height);
            zhugl.ShowDialog();
        }

        private void 司机管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        #region 临时车辆
        private void 车辆维修管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 添加临时车辆ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 管理临时车辆ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 生成临时密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 管理临时密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        #endregion      

        #endregion

        #region 轨迹播放

        //暂停、播放
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.button1.Text == "暂停")
            {
                this.timer5.Enabled = false;
                this.button1.Text = "播放";
            }
            else
            {
                this.timer5.Enabled = true;
                this.button1.Text = "暂停";
            }
        }
        //重放
        private void button2_Click(object sender, EventArgs e)
        {
            this.webBrowserMap.Document.InvokeScript("abc_removemarkc");
            this.webBrowserMap.Document.InvokeScript("abc_removePolyline");
            ls_guiji = 0;
            this.trackBar1.Value = 0;
            this.button1.Text = "暂停";
            panevisle(DTGjTable, DTGjssxxidt, this.comboBox1.Text, Hfdiansu, radioButtonName);
        }
       
        //取消
        private void button4_Click(object sender, EventArgs e)
        {
            this.webBrowserMap.Document.InvokeScript("abc_removemarkc");
            this.webBrowserMap.Document.InvokeScript("abc_removePolyline");
            this.timer5.Enabled = false;
            this.panel6.Visible = false;
        } 
        
        //导出
        private void button3_Click(object sender, EventArgs e)
        {
            string Cname = CurrentNode.Text;
            SaveFileDialog saveFileDialog;
            if (DTGjTable.Rows.Count == 0)
            {
                MessageBox.Show("没有数据可供导出！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Execl files(*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录 
                //saveFileDialog.CreatePrompt = true;
                saveFileDialog.Title = "导出Excel文件到";
                DateTime now = DateTime.Now;
                string enameStr = Cname + "历史轨迹报表" + now.Year.ToString().PadLeft(2) + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + "-" + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0');
                saveFileDialog.FileName = enameStr;

                //点了保存按钮进入   
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    if (saveFileDialog.FileName.Trim() == "")
                    {
                        MessageBox.Show("请输入要保存的文件名", "提示");
                        return;
                    }

                    Thread myThreadgj = new Thread(ExportTOExcel);
                    object ob = saveFileDialog.FileName;
                    myThreadgj.Start(ob);
                }
            }
        }
        public void ExportTOExcel(object exname)
        {
            DataTable dt = DTGjTable;
            ExportExcel_Class ec = new ExportExcel_Class();
            string returnStr = ec.ExportToExcel(dt, CurrentNode.Text, exname);
            MessageBox.Show(returnStr);
        }
        #endregion

        #region 设置

        #region 报警参数设置
        private void 全局设置ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Che_Settings.BaoJingCSSZ_GS gs = new XF_GPS.Che_Settings.BaoJingCSSZ_GS(us_zuzhi, sousuoTable);
            gs.ShowDialog();
        }

        private void 车辆单独设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Che_Settings.BaoJingCSSZ_CLDD cldd = new XF_GPS.Che_Settings.BaoJingCSSZ_CLDD(sousuoTable);
            cldd.ShowDialog();
        }
        #endregion

        #region 报警开关设置
        private void 全局设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Che_Settings.BaojingKG_QJ kgqj = new XF_GPS.Che_Settings.BaojingKG_QJ(sousuoTable, us_zuzhi);
            kgqj.ShowDialog();
        }

        private void 车辆设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Che_Settings.BaoJingKG_GSsz kggssz = new XF_GPS.Che_Settings.BaoJingKG_GSsz(sousuoTable, us_zuzhi);
            kggssz.ShowDialog();
        }

        private void 公司单独设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Che_Settings.BaoJingKG_CLsz kgclsz = new XF_GPS.Che_Settings.BaoJingKG_CLsz(sousuoTable, us_zuzhi);
            kgclsz.ShowDialog();
        }
        #endregion

        #region 监控区管理
        private void 新增监控区域ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowserMap.Document.InvokeScript("abc_lk_cm");
        }
        public void addquyu()
        {
            Che_Settings.AddJianKong_QY addqy = new XF_GPS.Che_Settings.AddJianKong_QY(this.webBrowserMap);
            addqy.ShowDialog();
        }

        private void 监控区域设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Che_Settings.JianKongQY_SZ jkqysz = new XF_GPS.Che_Settings.JianKongQY_SZ(sousuoTable);
            jkqysz.ShowDialog();
        }

        private void 监控区域管理ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Che_Settings.JianKongQY_GuanLi chaosu = new Che_Settings.JianKongQY_GuanLi(sousuoTable);
            int x = webBrowserMap.PointToScreen(new Point()).X - 5;
            int y = webBrowserMap.PointToScreen(new Point()).Y - 5;
            chaosu.Location = new Point(x, y);
            int height = webBrowserMap.Size.Height;
            chaosu.Size = new Size(webBrowserMap.Size.Width + 5, webBrowserMap.Size.Height);
            chaosu.ShowDialog();
        }

        private void 显示监控区域ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        #endregion

        private void 监控权限设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 监控站点设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region 监控报警实时信息
        private void timer6_Tick(object sender, EventArgs e)
        {
            //System.Data.DataTable dtssxx = new System.Data.DataTable();
            //string csz = ""; //超速值
            //string wxhjg = "";//无信号间隔
            //string lcbhjg = ""; //里程变化间隔
            //string tccsjg = "";//停车超时间隔
            //string jjsjg = "";//加减速间隔
            //string ylbhjg = "";//油量变化间隔
            //string liczsbj = "";//里程总数报警
            
            //for (int i = 0; i < sousuoTable.Rows.Count; i++)
            //{
            //    int compid = int.Parse(sousuoTable.Rows[i]["CID"].ToString());
            //    System.Data.DataTable dts = SqlHelper.Sqlselect("select * from GSBJ_CSSZ where CID=" + compid + "", "GPCTable");
            //    if (dts.Rows.Count > 0)
            //    {
            //    for (int j = 0; j < dts.Rows.Count; j++)
            //    {
            //        if (int.Parse(dts.Rows[j]["CID2"].ToString()) < 1 || int.Parse(dts.Rows[j]["CID2"].ToString()) == 8 || int.Parse(dts.Rows[j]["CID2"].ToString()) == 10 || int.Parse(dts.Rows[j]["CID2"].ToString()) == 11)
            //        {
            //        }
            //        else
            //        {
                        
            //                csz = dts.Rows[0]["bjcs_csz"].ToString();
            //                wxhjg = dts.Rows[0]["bjcs_wxhjg"].ToString();
            //                lcbhjg = dts.Rows[0]["bjcs_lcbhjg"].ToString();
            //                tccsjg = dts.Rows[0]["bjcs_tccsz"].ToString();
            //                jjsjg = dts.Rows[0]["bjcs_jjsjg"].ToString();
            //                ylbhjg = dts.Rows[0]["bjcs_ylbhjg"].ToString();
            //                liczsbj = dts.Rows[0]["bjcs_lczsbj"].ToString();
            //            }


            //        }
            //    }
            //}
        }
        #endregion

        #region 报警实时信息
        DataTable BaoJingTable = null;
        string BCdate = "";
        Baojing_Shisxx bjFrom;
        public void BaoJing_ssxx()
        {
            try
            {
                //DataTable dzuz = sousuoTable;
                //string date = DateTime.Now.Date.ToString("yyyyMMdd");

                //string DQdate = DateTime.Now.Date.ToString("yyyy-MM-dd") + " " + DateTime.Now.ToLongTimeString();
                //if (BCdate == "")
                //{
                //    BCdate = DQdate;
                //}
                //mywebservice.ServiceSoapClient myss = new XF_GPS.mywebservice.ServiceSoapClient();

                //for (int i = 0; i < dzuz.Rows.Count; i++)
                //{
                //    int zz_cid = int.Parse(dzuz.Rows[i]["CID"].ToString());
                //    System.Data.DataTable dts = SqlHelper.Sqlselect("select * from COMPANY INNER JOIN GROUPS ON COMPANY.cid = GROUPS.cid INNER JOIN PERSONINFO ON PERSONINFO.cpid = GROUPS.gid  where COMPANY.[CID] = " + zz_cid + "", "GPCTable");
                //    if (dts.Rows.Count > 0)
                //    {
                //        for (int j = 0; j < dts.Rows.Count; j++)
                //        {
                //            string chepai_id = dts.Rows[j]["PID"].ToString();
                //            DataTable chepaidt = SqlHelper.Sqlselect("select * from GSBJ_CSSZ where bjcs_cpid !='" + chepai_id + "'", "gsbj_ccszs");
                //            if (chepaidt.Rows.Count > 0)
                //            {

                //                string csz = chepaidt.Rows[0]["bjcs_csz"].ToString();//超速值
                //                string wxhjg = chepaidt.Rows[0]["bjcs_wxhjg"].ToString();//无信号间隔
                //                string lcbhjg = chepaidt.Rows[0]["bjcs_lcbhjg"].ToString();//里程变化间隔
                //                string tccsjg = chepaidt.Rows[0]["bjcs_tccsz"].ToString();//停车超时间隔
                //                string jjsjg = chepaidt.Rows[0]["bjcs_jjsjg"].ToString();//加减速间隔
                //                string ylbhjg = chepaidt.Rows[0]["bjcs_ylbhjg"].ToString();//油量变化间隔
                //                string liczsbj = chepaidt.Rows[0]["bjcs_lczsbj"].ToString();//里程总数报警

                //                if (csz != "" && csz != "0")
                //                {
                //                    string SQLStr="select * from data2_" + date + "_1 where pid=" + int.Parse(chepai_id) + " and ctime>to_date('" + BCdate + "','yyyy-mm-dd hh24:mi:ss')";
                //                    DataTable dttime = myss.Sqlselect(SQLStr, "" + date + "Tablename");
                //                    if (dttime.Rows.Count > 0)
                //                    {
                //                        for (int k = 0; k < dttime.Rows.Count; k++)
                //                        {
                //                            string cph = dts.Rows[j]["CARCODE"].ToString();//车牌号
                //                            string bjlx = "";//报警类型
                //                            string sd = dttime.Rows[k]["SPEED"].ToString();//速度
                //                            string sj = dts.Rows[j]["PNAME"].ToString();//司机
                //                            string bjsj = dttime.Rows[k]["CTIME"].ToString();//报警时间
                //                            string beizhu = "";//备注
                //                            string lic = dttime.Rows[k]["MILEAGE"].ToString(); //总里程


                //                            if (double.Parse(sd) > double.Parse(csz)) //超速
                //                            {
                //                                DataRow dr2 = BaoJingTable.NewRow();
                //                                dr2["cph"] = cph;
                //                                dr2["bjlx"] = "超速报警";
                //                                dr2["sd"] = sd;
                //                                dr2["sj"] = sj;
                //                                dr2["bjsj"] = bjsj;
                //                                dr2["bz"] = beizhu;
                //                                BaoJingTable.Rows.Add(dr2);
                //                            }

                //                            //if (liczsbj != "" && liczsbj != "0")
                //                            //{
                //                            //    if (double.Parse(lic) > double.Parse(liczsbj)) //里程
                //                            //    {
                //                            //        DataRow dr3 = BaoJingTable.NewRow();
                //                            //        dr3["cph"] = cph;
                //                            //        dr3["bjlx"] = "里程总数报警";
                //                            //        dr3["sd"] = sd;
                //                            //        dr3["sj"] = sj;
                //                            //        dr3["bjsj"] = bjsj;
                //                            //        dr3["bz"] = "里程总数：" + lic;
                //                            //        BaoJingTable.Rows.Add(dr3);
                //                            //    }
                //                            //}
                //                        }
                //                    }
                //                }
                //            }
                //        }
                //    }
                //}
                //BCdate = DQdate;
                //if (BaoJingTable.Rows.Count > 0)
                //{
                    Baojing_Shisxx bjssxx = new Baojing_Shisxx(BaoJingTable);
                    int y = this.Height - bjssxx.Height;
                    int x = this.Width - bjssxx.Width;
                    bjssxx.Location = new Point(x - 9, y - 17);

                    if (bjFrom == null)
                    {
                        bjssxx.Show();
                    }
                    else
                    {
                        if (bjFrom.Created)   //控件是否已经创建 
                        {
                            //MessageBox.Show("已打开");
                            //bjFrom.Activate();
                            bjssxx.BindGV(BaoJingTable);
                        }
                        else
                        {
                            bjssxx.Show();
                        }
                    }
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void timer7_Tick(object sender, EventArgs e)
        {
            //Thread threadReceives = new Thread(new ThreadStart(BaoJing_ssxx));
            //threadReceives.Start();
        }
        #endregion



    }
}
